﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NFox.Basal.String
{
    /// <summary>
    /// 字符处理类
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// 将指定的字符串集用参数<code>splitString</code>作为分割符合并为一个字符串。
        /// </summary>
        /// <param name="splitString">字符串分格符</param>
        /// <param name="strings">要合并的字符串集合</param>
        /// <returns>返回合并后的字符串</returns>
        public static string MergeString(string splitString, params string[] strings)
        {
            if (strings == null)
            {
                return string.Empty;
            }
            string pFileStr = strings.First();
            for (int i = 1; i < strings.Length; i++)
            {
                string pPrefixString = MergeString(splitString, strings[i], false);
                pFileStr = MergeString(pFileStr, pPrefixString, false);
            }
            //foreach (string str in strings)
            //{
            //    pFileStr += string.IsNullOrWhiteSpace(pFileStr) ? str : MergeString(splitString, str, false);
            //}
            return pFileStr;
        }
        /// <summary>
        /// 将指定的字符串集用参数<code>splitString</code>作为分割符合并为一个字符串。
        /// </summary>
        /// <param name="splitString">字符串分格符</param>
        /// <param name="strings">要合并的字符串集合</param>
        /// <returns>返回合并后的字符串</returns>
        public static string MergeString(string splitString, IEnumerable<string> strings)
        {
            if (strings == null)
            {
                return string.Empty;
            }
            return MergeString(splitString, strings.ToArray());
        }

        /// <summary>
        /// 合并两个字符串
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <param name="appendString">要添加的字符串</param>
        /// <param name="isPrefix">appendString是否为前缀？如果为true,则添加到sourceString前，否则添加到sourceString后。</param>
        /// <returns>返回合并后的字符串</returns>
        public static string MergeString(string sourceString, string appendString, bool isPrefix)
        {
            return MergeString(sourceString, appendString, string.Empty, isPrefix);
        }
        /// <summary>
        /// 合并两个字符串
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <param name="appendString">要添加的字符串</param>
        /// <param name="splitString">字符串分格符</param>
        /// <param name="isPrefix">appendString是否为前缀？如果为true,则添加到sourceString前，否则添加到sourceString后，同时用splitString进行分割。</param>
        /// <returns>返回合并后的字符串</returns>
        public static string MergeString(string sourceString, string appendString, string splitString, bool isPrefix)
        {
            string pOneString = isPrefix ? appendString : sourceString;
            string pTwoString = isPrefix ? sourceString : appendString;
            return $"{pOneString}{splitString}{pTwoString}";
        }
        /// <summary>
        /// 将字符串中指定的旧字符替换为指定的新字符。
        /// </summary>
        /// <param name="sourceString">原始字符串</param>
        /// <param name="newValue">新字符</param>
        /// <param name="oldValues">旧字符集</param>
        /// <returns>返回替换后的字符串。</returns>
        public static string Replace(this string sourceString, char newValue, params char[] oldValues)
        {
            if (sourceString == null || oldValues == null)
            {
                return sourceString;
            }
            string pSourceString = sourceString;
            foreach (char oldValue in oldValues)
            {
                pSourceString = pSourceString.Replace(oldValue, newValue);
            }
            return pSourceString;
        }
        /// <summary>
        /// 将字符串中指定的旧字符替串换为指定的新字符串。
        /// </summary>
        /// <param name="sourceString">原始字符串</param>
        /// <param name="newValue">新字符串</param>
        /// <param name="oldValues">旧字符串集</param>
        /// <returns>返回替换后的字符串。</returns>
        public static string Replace(this string sourceString, string newValue, params string[] oldValues)
        {
            if (sourceString == null || oldValues == null)
            {
                return sourceString;
            }
            string pSourceString = sourceString;
            foreach (string oldValue in oldValues)
            {
                pSourceString = pSourceString.Replace(oldValue, newValue);
            }
            return pSourceString;
        }
    }
}
